package com.tanhua.dubbo.api;

import com.tanhua.model.enums.SoundType;
import com.tanhua.model.mongo.UserSound;
import com.tanhua.model.mongo.UserSoundTime;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * @Description: test
 * @Create by: 熊孩子
 * @Date: 2021/12/12 16:20
 */
@DubboService
public class UserSoundTimeApiImpl implements UserSoundTimeApi{

    @Autowired
    private MongoTemplate mongoTemplate;
    //查询语音剩余次数对象
    @Override
    public UserSoundTime queryUserSoundTime(Long userId, String date, SoundType soundType) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("date").is(date).and("soundType").is(soundType.getType()));
        return mongoTemplate.findOne(query,UserSoundTime.class);
    }
    //保存当前用户的语音接收次数
    @Override
    public void save(UserSoundTime userSoundTime) {
        mongoTemplate.save(userSoundTime);
    }
    //修改当前用户的语音接收次数
    @Override
    public void update(UserSoundTime userSoundTime) {
        Query query = Query.query(Criteria.where("userId").is(userSoundTime.getUserId())
                .and("date").is(userSoundTime.getDate()).and("soundType").is(SoundType.RECEIVE.getType()));
        Update update = new Update();
        update.inc("remainingTimes",-1);
        mongoTemplate.updateFirst(query,update,UserSoundTime.class);
    }
}
